package com.cskaoyan.service.back;

import com.cskaoyan.bean.po.BasePageInfo;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.bean.vo.back.AdminCreateVO;
import com.cskaoyan.bean.vo.back.AdminListVO;
import com.cskaoyan.bean.vo.back.AdminUpdateVO;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.util.Md5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 12-江涛
 * @since 2022/10/12  13:21
 */
@Service
public class AdminManagerServiceImpl implements AdminManagerService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public CommonData<AdminListVO> adminList(BasePageInfo basePageInfo,String username) {

        //查表
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        //1.查询
        marketAdminExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (username != null) {
            criteria.andUsernameEqualTo(username);
        }
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);

        //分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(marketAdmins);

        List<AdminListVO> adminListVO = new ArrayList<>();
        for (MarketAdmin marketAdmin : marketAdmins) {

            AdminListVO adminListVO1 = new AdminListVO();
            adminListVO1.setId(marketAdmin.getId());
            adminListVO1.setUsername(marketAdmin.getUsername());
            adminListVO1.setAvatar(marketAdmin.getAvatar());
            adminListVO1.setRoleIds(marketAdmin.getRoleIds());
            adminListVO.add(adminListVO1);
        }


        CommonData<AdminListVO> commonDataVO = new CommonData<>();
        commonDataVO.setLimit(basePageInfo.getLimit());
        commonDataVO.setPage(basePageInfo.getPage());
        commonDataVO.setPages(pageInfo.getPages());
        commonDataVO.setTotal((int) pageInfo.getTotal());
        commonDataVO.setList(adminListVO);

        return commonDataVO;
    }

    @Override
    public AdminCreateVO adminCreate(MarketAdmin marketAdmin) {

        //判断用户名是否重复
        int count1 = marketAdminMapper.selectByusername(marketAdmin.getUsername());
        if (count1 == 1) {
            return null;
        }

        //处理密码
        String passwordMd5 = null;
        try {
            passwordMd5 = Md5Util.getMd5(marketAdmin.getPassword(), "抗议小组");
        } catch (Exception e) {
            e.printStackTrace();
        }

        int count = marketAdminMapper.insertSelective(marketAdmin);

        AdminCreateVO adminCreateVO = new AdminCreateVO();
        if (count == 1) {

            adminCreateVO.setId(marketAdmin.getId());
            adminCreateVO.setUsername(marketAdmin.getUsername());
            adminCreateVO.setPassword(passwordMd5);
            adminCreateVO.setAvatar(marketAdmin.getAvatar());
            adminCreateVO.setRoleIds(marketAdmin.getRoleIds());
            Date date = new Date();
            adminCreateVO.setAddTime(date);
            adminCreateVO.setUpdateTime(date);

            return adminCreateVO;
        }

        return null;
    }

    @Override
    public AdminUpdateVO adminUpdate(MarketAdmin marketAdmin) {

        //update表
        int count = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);

        if (count == 1) {

            AdminUpdateVO adminUpdateVO = new AdminUpdateVO();

            adminUpdateVO.setId(marketAdmin.getId());
            adminUpdateVO.setUsername(marketAdmin.getUsername());
            adminUpdateVO.setAvatar(marketAdmin.getAvatar());
            adminUpdateVO.setAddTime(marketAdmin.getAddTime());
            adminUpdateVO.setUpdateTime(marketAdmin.getUpdateTime());
            adminUpdateVO.setRoleIds(marketAdmin.getRoleIds());

            return adminUpdateVO;
        }

        return null;
    }

    @Override
    public int adminDelete(MarketAdmin marketAdmin) {

        MarketAdmin marketAdmin1 = new MarketAdmin();
        marketAdmin1.setId(marketAdmin.getId());
        marketAdmin1.setDeleted(true);

        int count = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin1);

        return count;

    }
}
